<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="titleBar">
            <img class="logo" src="../assets/logo.png" alt="logo" />
            <div class="title">葵谟云枢</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClik"
        />
      </a-col>
      <a-col flex="120px">
        <div class="userLoginStatus">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </ASpace>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                    <LogoutOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <router-link to="/user/login">
              <a-button type="primary">登录</a-button>
            </router-link>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, h, onMounted, ref } from 'vue'
import type { MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'

const current = ref<string[]>(['mail'])

// 菜单列表
const originItems = [
  {
    key: '/',
    label: '主页',
    title: '主页',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/spaceManage',
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: '/admin/userManage',
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/other',
    label: h('a', { href: 'https://gitee.com/lqy125130', target: '_blank' }, '详情'),
    title: 'Gitee',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  // @ts-ignore
  return menus?.filter((menu) => {
    // @ts-ignore
    if (menu?.key.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
// @ts-ignore
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

const loginUserStore = useLoginUserStore()

const router = useRouter()

/**
 * 菜单点击事件
 * @param key 菜单key
 */

// @ts-ignore
const doMenuClik = ({ key }) => {
  router.push({
    path: key,
  })
}

// 路由变化时，更新当前选中的菜单
router.afterEach((to) => {
  current.value = [to.path]
})

// 用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  console.log(res)
  if (res.code === 0) {
    loginUserStore.setLoginUser({
      userName: '未登录',
    })
    message.success('退出登录成功')
    await router.push('/user/login')
  } else {
    message.error('退出登录失败，' + res.message)
  }
}
</script>

<style scoped>
#globalHeader .titleBar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
  width: 48px;
}
</style>
